package com.knowledgeqa.system.service.impl;

import com.knowledgeqa.system.entity.CoinTransaction;
import com.knowledgeqa.system.mapper.CoinTransactionMapper;
import com.knowledgeqa.system.service.CoinTransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 金币交易服务实现类
 */
@Service
public class CoinTransactionServiceImpl implements CoinTransactionService {

    @Autowired
    private CoinTransactionMapper coinTransactionMapper;

    @Override
    public boolean createTransaction(Long userId, Integer amount, Integer type, String remark) {
        CoinTransaction transaction = new CoinTransaction();
        transaction.setUserId(userId);
        transaction.setAmount(amount);
        transaction.setType(type);
        transaction.setRemark(remark);
        transaction.setCreateTime(new Date());
        
        return coinTransactionMapper.insert(transaction) > 0;
    }

    @Override
    public List<CoinTransaction> findByUserId(Long userId) {
        return coinTransactionMapper.selectByUserId(userId);
    }

    @Override
    public List<CoinTransaction> findRechargeRecords(Long userId) {
        // 0表示充值类型
        return coinTransactionMapper.selectByUserIdAndType(userId, 0);
    }

    @Override
    public List<CoinTransaction> findConsumeRecords(Long userId) {
        // 1、2、3表示不同的消费类型
        List<CoinTransaction> consumeRecords = coinTransactionMapper.selectByUserIdAndType(userId, 1);
        consumeRecords.addAll(coinTransactionMapper.selectByUserIdAndType(userId, 2));
        consumeRecords.addAll(coinTransactionMapper.selectByUserIdAndType(userId, 3));
        return consumeRecords;
    }

    @Override
    public List<CoinTransaction> findAll() {
        return coinTransactionMapper.selectAll();
    }
}